package com.yonyou.pmclouds.discuss.itf;

import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.discuss.entity.DiscussNoticeVO;
import com.yonyou.pmclouds.discuss.entity.DiscussionVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class AbstractSpecifiedTypeDiscussProcessor implements SpecifiedTypeDiscussProcessor {
    protected static final String COMMENT_MESSAGE = "评论";
    protected static final String REPLY_MESSAGE = "回复";
    public static final int COMMENT_TYPE = 0;
    public static final int REPLY_TYPE = 1;
    public static final String REGEX = "\\^@\\{\"pkUser\":\"(.+?)\",\"userName\":\"(.+?)\"}\\^";
    /**
     * 匹配@的正则表达式
     */
    protected static final Pattern PATTERN = Pattern.compile(REGEX);
    @Autowired
    private DiscussInternalOperation discussOperation;

    public DiscussNoticeVO[] generateNotice(DiscussionVO discussion) throws BusinessException {
        Map<String, String> users = getUserTitle(discussion);
        if (users == null || users.isEmpty()) {
            return null;
        }
        DiscussNoticeVO[] result = new DiscussNoticeVO[users.size()];
        int i = 0;
        for (Map.Entry<String, String> entry : users.entrySet()) {
            result[i] = new DiscussNoticeVO();
            result[i].setPkDiscuss(discussion.getPkDiscuss());
            result[i].setPkTenant(discussion.getPkTenant());
            result[i].setRelateKey(discussion.getRelateKey());
            result[i].setRelateType(discussion.getRelateType());
            result[i].setHasRead(false);
            result[i].setReceiver(entry.getKey());
            result[i].setTitle(entry.getValue());
            i++;
        }
        return result;
    }

    protected DiscussionVO getRepliedDiscuss(DiscussionVO discussion) {
        if (StringUtils.isEmpty(discussion.getRepliedDiscuss())) {
            return null;
        }
        return discussOperation.queryByDiscussId(discussion.getRepliedDiscuss());
    }

    protected int getDiscussType(DiscussionVO discussion) {
        if (StringUtils.isEmpty(discussion.getRepliedDiscuss())) {
            return COMMENT_TYPE;
        } else {
            return REPLY_TYPE;
        }
    }

    protected String getDiscussTypeMessage(int type) {
        if (type == COMMENT_TYPE) {
            return COMMENT_MESSAGE;
        } else {
            return REPLY_MESSAGE;
        }
    }

    /**
     * 解析出被@的用户
     */
    protected String[] extractMentionedUser(String content) {
        Matcher matcher = PATTERN.matcher(content);
        List<String> mentioned = new ArrayList<>();
        while (matcher.find()) {
            String user = matcher.group(1);
            mentioned.add(user);
        }
        return mentioned.toArray(new String[0]);
    }

    /**
     * 返回评论需要通知的用户及对应的title
     */
    protected abstract Map<String, String> getUserTitle(DiscussionVO discussion) throws BusinessException;
}
